package com.kaiyidiannao.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import com.kaiyidiannao.activity.auction.AuctionActivity;
import com.kaiyidiannao.common.BaseActionBarActivity;
import com.kaiyidiannao.data.DataCenter;
import com.kaiyidiannao.main.R;
import com.kaiyidiannao.model.bean.Auction;
import com.kaiyidiannao.model.req.model.PostDeleteMessageInfo;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 闯儿 on 14-4-2.
 */
public class AuctionAdapter extends ImageLoaderBaseAdapter {
    private ImageLoader imageLoader;
    private LayoutInflater layoutInflater;
    private List<Auction> list = new ArrayList<Auction>();
    private Context context;
    private boolean isFav;

    private ArrayList<Auction> beans = DataCenter.auctionArrayList;

    /**
     * @param isFav true表示的是用户中心，false表示的是拍拍乐列表页面
     */
    public AuctionAdapter(Context context, List<Auction> list, boolean isFav) {
        imageLoader = ImageLoader.getInstance();
        imageLoader.init(ImageLoaderConfiguration.createDefault(context));
        this.list = list;
        this.layoutInflater = LayoutInflater.from(context);
        this.context = context;
        this.isFav = isFav;
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        TextView nameTextView;
        TextView trueName;
        TextView addPriceTimes;
        TextView addPeopleTimes;
        ImageButton delete = null;

        Auction auction = list.get(position);
        if (convertView == null) {
            if (isFav) {//用户中心 - 拍拍乐列表页面
                convertView = layoutInflater.inflate(R.layout.auction_item_myfav, null);
            } else { //拍拍乐列表页面
                convertView = layoutInflater.inflate(R.layout.auction_item, null);
            }
        }
        imageView = (ImageView) convertView.findViewById(R.id.auction_item_ImageView);
        nameTextView = (TextView) convertView.findViewById(R.id.auction_item_nameTextView);
        trueName = (TextView) convertView.findViewById(R.id.auction_item_addNumTextView);
        addPriceTimes = (TextView) convertView.findViewById(R.id.auction_item_addManNumTextView);
        addPeopleTimes = (TextView) convertView.findViewById(R.id.auction_item_addManNumTextViewT);

        delete = (ImageButton) convertView.findViewById(R.id.btDelete);
        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Auction bean2 = beans.get(position);
                String action;
                action = "delete";
                DataCenter.position = position;
                String userid = DataCenter.userLogin.get(0).getUserid();
                ((BaseActionBarActivity) context).showProgressDialog();
                new PostDeleteMessageInfo(bean2.getFid(), userid, action).execute();
            }
        });

        imageLoader.displayImage(auction.getThumb(), imageView, options, null);
        // 设置图片大小
        if (!isFav) { //拍拍乐列表页面
            super.ajastImageViewSize(imageView, auction.getThumb(), ((AuctionActivity) context).getmXListView(), context);
        }

        nameTextView.setText(auction.getTitle());
        trueName.setText(auction.getUsername()); //名字,不能透露用户的真实姓名
        addPriceTimes.setText(auction.getCount()); //加价次数，
        addPeopleTimes.setText(auction.getPeople() + "");//加价人数

        return convertView;
    }
}
